package walkera.cn.httpserve.util;


import android.support.annotation.NonNull;
import android.util.Log;

import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpLogTool   implements HttpLoggingInterceptor.Logger {

    private StringBuilder mMessage = new StringBuilder();

    @Override
    public void log(@NonNull String message) {
        // 请求或者响应开始
        if (message.startsWith("--> POST") ||  message.startsWith("--> GET") ) {
            mMessage.setLength(0);
        }
        // 以{}或者[]形式的说明是响应结果的json数据，需要进行格式化
        if ((message.startsWith("{")
                && message.endsWith("}"))
                || (message.startsWith("[")
                && message.endsWith("]"))) {
            message = JsonUtil.formatJson(JsonUtil.decodeUnicode(message));
        }

        mMessage.append(message.concat("\n"));

        if (message.startsWith("<-- END HTTP")) {
            Log.d("OkHttpLogInfo: ", mMessage.toString());
        }
    }//
}
